<template>
	<view>
    <!-- 自定义搜索组件 -->
    <my-search/>
    
		<view class="scroll-container">
      <!-- 左侧滑动 -->
      <scroll-view scroll-y :style="{height: currentHeight + 'px'}" class="left-scroll">
        <block v-for="(item,index) in CateList" :key="index">
          <view :class="['left-scroll-item', index === active ? 'active' : '']" @click="changeActive(index)">{{item.cat_name}}</view>
        </block>
      </scroll-view>
      
      <!-- 右侧滑动 -->
      <scroll-view scroll-y :style="{height: currentHeight + 'px'}" :scroll-top="scrollTop">
        <view class="cate-level2" v-for="(item,index) in CateList_two" :key="index">
          <!-- 二级分类标题 -->
          <view class="cate-level2-title">/ {{item.cat_name}} /</view>
          <view class="cate-level3-list">
            <view class="cate-level3-item" v-for="(item,index) in item.children" :key="index" @click="gotoGoodsList(item)">
              <image :src="item.cat_icon"></image>
              <text>{{item.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
	</view>
</template>

<script>
  import {CateListData} from '../../service/cate_api.js'
  import mySearch from '../../components/my-search.vue'
  import badgeMix from '../../mixins/tabbar-badge.js'
	export default {
    mixins:[badgeMix],
    components:{
      mySearch
    },
		data() {
			return {
				currentHeight:0,
        CateList:[],
        CateList_two:[],
        active:0,
        scrollTop:0
			};
		},
    onLoad(){
      // 获取当前设备可用窗口高度
      const res = uni.getSystemInfoSync()
      //除去搜索框50px的高度
      this.currentHeight = res.windowHeight - 50
      //获取分类页面数据
      this.getPageData()
      
    },
    methods:{
      getPageData(){
        this.getCateListData()
      },
      //获取左侧导航分类数据
      async getCateListData(){
        const res = await CateListData()
        this.CateList = res.message
        this.CateList_two = res.message[0].children
      },
      //动态切换左栏分类
      changeActive(index){
        this.active = index
        //动态获取二级分类内容
        this.CateList_two = this.CateList[index].children
        //动态更改三级分类scroll位置
        this.scrollTop = this.scrollTop === 0 ? 1 : 0
        
      },
      //跳转到商品列表
      gotoGoodsList(item){
        uni.navigateTo({
          url:"/subpkg/goods_list/goods_list?cid=" + item.cat_id
        })
      }
    }
	}
</script>

<style lang="scss">
.scroll-container{
  display: flex;
    
  .left-scroll{
    width:120px;
  }
  .left-scroll-item{
    background-color: #F7F7F7;
    line-height: 60px;
    text-align: center;
    font-size:12px;
    
    &.active{
      background-color: #ffffff;
      position:relative;
      &::before{
        content:' ';
        display: block;
        width:3px;
        height:30px;
        background-color: #C00000;
        position:absolute;
        top:50%;
        left:0;
        transform: translateY(-50%);
        
      }
    }
  }
}

.cate-level2-title{
  font-size:14px;
  font-weight: 700;
  text-align: center;
  padding:15px 0;
}

.cate-level3-list{
  display:flex;
  flex-wrap:wrap;
  .cate-level3-item{
    width:33.33%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    margin-bottom:10px;
    image{
      width:60px;
      height:60px;
    }
    text{
      font-size:12px;
    }
  }
}
</style>
